package com.gombe.credit.app.ui.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import com.gombe.credit.app.R
import com.gombe.credit.app.common.CommonDialog
import com.gombe.credit.app.databinding.ActGombeCreditPermissionsChildItemBinding
import com.gombe.credit.app.databinding.DialogGombeCreditPermissionReasonBinding
import com.gombe.credit.app.event.GombeCreditEventPointType
import org.json.JSONObject

/**
 * GombeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/13 15:41
 * @Annotation：权限说明 dialog
 */
class DialogGombeCreditPermissionReason: CommonDialog {

    private lateinit var binding: DialogGombeCreditPermissionReasonBinding
    private var mCallback: IDialogCallback? = null
    private var reasonItemData: JSONObject? = null
    //事件备注
    private var eventRemark = ""

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context, remark: String) : super(context) {
        eventRemark = remark
    }
    constructor(context: Context, remark: String, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun takeGombeCreditRootView(): View {
        binding = DialogGombeCreditPermissionReasonBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initGombeCreditView() {
    }

    override fun initGombeCreditListener() {
        setOnClickListener(binding.btnDeny) //取消
        setOnClickListener(binding.btnAgree) //ok
    }

    override fun takeGombeCreditData() {
    }

    override fun onGombeCreditViewClick(v: View?) {
        if (v?.id == R.id.btnAgree){
            if (!binding.permissionReadCheckBox.isChecked){
                showRequestPermissionTipsDialog()
                return
            }
        }
        dismiss()
        mCallback?.onClick(v)
    }

    /**
     * 设置埋点备注
     */
    fun addGombeCreditReasonEventRemark(remark: String){
        eventRemark = remark
    }

    /**
     * 设置权限
     */
    fun showGombeCreditPermissionReasonData(data: JSONObject){
        reasonItemData = data
        binding.imageIcon.setImageResource(data.optInt("icon"))
        binding.textPermissionTitle.text = data.optString("title")

        binding.reasonLayout.removeAllViews()
        val itemList = data.optJSONArray("msgList")
        itemList?.let {
            for (i in 0..< itemList.length()){
                val item = itemList.optJSONObject(i)
                val itemView = ActGombeCreditPermissionsChildItemBinding.inflate(layoutInflater)
                val showTitle = item.optString("title")
                if (showTitle.isNotEmpty()){
                    itemView.textTitle.text = showTitle
                    itemView.textTitle.visibility = View.VISIBLE
                } else {
                    itemView.textTitle.visibility = View.GONE
                }
                itemView.textReason.text = item.optString("des")
                binding.reasonLayout.addView(itemView.root)
            }
        }
    }

    /**
     * 提示dialog
     */
    private fun showRequestPermissionTipsDialog(){
        reasonItemData?.let {
            val requestPermission = it.optString("permission")
            val dialog = DialogGombeCreditTips(context,requestPermission, object : DialogGombeCreditTips.IDialogCallback{
                override fun onClick(view: View?) {
                    when(view?.id){
                        R.id.buttonConfirm -> {
                            addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_CLICK.PERMISSION_REASON_DIALOG_AGREE,requestPermission)
                            mCallback?.onClick(binding.btnAgree)
                            dismiss()
                        }
                        R.id.buttonCancel -> {
                            addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_CLICK.PERMISSION_REASON_DIALOG_DENY,requestPermission)
                        }
                    }
                }
            })
            dialog.setCancelable(true)
            dialog.setCanceledOnTouchOutside(true)
            dialog.show()
            dialog.setGombeCreditKindTipsIconResource(R.mipmap.gombe_credit_image_dialog_tips_icon)
            dialog.setGombeCreditKindTipsTitleText(getString(R.string.gombe_credit_dialog_kind_tips_title),getColor(R.color.gombe_credit_ff1d1e20))
            dialog.setGombeCreditKindTipsMsgText(getString(R.string.gombe_credit_permission_dialog_agree_tips))
            dialog.setGombeCreditKindTipsMsgGravity(Gravity.CENTER)
            dialog.isGombeCreditKindTipsDoubleButton(true)
            dialog.setGombeCreditKindTipsButtonCancelText(getString(R.string.gombe_credit_button_cancel))
            dialog.setGombeCreditKindTipsButtonCancelBackgroundResource(getColor(R.color.gombe_credit_FFF9F9F7))
            dialog.setGombeCreditKintTipsButtonConfirmText(getString(R.string.gombe_credit_button_ok))
            dialog.setGombeCreditKindTipsButtonConfirmBackgroundResource(getColor(R.color.theme))
        }
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_DIALOG.PERMISSION_REASON_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_DIALOG.PERMISSION_REASON_DISMISS,eventRemark)
    }
}